<template>
  <div>
    <!--  面包屑  -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item><span>订单</span></el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!--  搜索框  -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容" v-model="this.queryInfo.query">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!--  订单数据表格  -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" prop="order_id"></el-table-column>
        <el-table-column label="订单编号" prop="order_number" width="300px"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="150px"></el-table-column>
        <el-table-column label="是否付款" prop="pay_status"  width="150px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status === '0'" type="danger">未付款</el-tag>
            <el-tag v-else type="success">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" width="150px">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_send === '是'" type="success">是</el-tag>
            <el-tag v-else type="danger">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="250px">
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>

        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-tooltip class="item" effect="dark" content="编辑" open-delay="300" enterable placement="top">
            <el-button type="primary" size="mini" @click="showEditDialog" icon="el-icon-edit"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" open-delay="300" content="订单详情" enterable placement="top">
            <el-button type="success" size="mini" @click="showProgress" icon="el-icon-location"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--  分页区  -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!--  修改地址对话框  -->
    <el-dialog
      title="修改地址"
      :visible.sync="editCityVisible"
      width="500px"
      @close="editDialogClosed">
      <el-form ref="editCityRef" :model="editCity" :rules="editCityRules" label-width="100px">
        <el-form-item label="省市区/县" prop="city">
          <el-cascader :options="cityData" v-model="editCity.city">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="editCity.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="editCityVisible = false">取 消</el-button>
    <el-button type="primary">确 定</el-button>
  </span>
    </el-dialog>

    <!--  订单详情对话框  -->
    <el-dialog
      title="物流进度"
      :visible.sync="progressVisible"
      width="70%">
      <!--  物流进度时间戳  -->
      <el-timeline>
        <el-timeline-item :timestamp="progress.time"  :key="index" v-for="(progress,index) in progressInfo" placement="top">
          <el-card>
            <h4>{{progress.context}}</h4>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';
import cityData from './citydata';

export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      orderList: [],
      // 修改地址对话框是否可见
      editCityVisible: false,
      editCity: {
        city: [],
        address: ''
      },
      editCityRules: {
        city: [
          { required: true, message: '请选择城市', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      cityData,
      progressVisible: false,
      progressInfo: []

    };
  },
  created () {
    this.getOrderList();
  },
  methods: {
    async getOrderList () {
      const { data: res } = await axios.get(
        'orders',
        { params: this.queryInfo });
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单失败!');
      }
      this.total = res.data.total;
      this.orderList = res.data.goods;
    },

    // 监听 pageSize 改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
    editDialogClosed () {
      this.$refs.editCityRef.resetFields();
    },
    showEditDialog () {
      this.editCityVisible = true;
    },
    async showProgress () {
      // 供测试的物流单号：1106975712662
      const { data: res } = await axios.get('/kuaidi/1106975712662');
      console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败!');
      }
      this.progressInfo = res.data;
      this.progressVisible = true;
    }

  },
  computed: {

  }
};
</script>

<style lang="less" scoped>
span{
  font-weight: bold;
  color:#409eff
}
.el-cascader {
  width: 100%;
}
</style>
